package com.vanysoft.business.system.licenseManager.controller;

import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.system.licenseManager.controller.demo.LicenseObj;
import com.vanysoft.business.system.licenseManager.entity.TAuthorizeProduct;
import com.vanysoft.business.system.licenseManager.entity.TAuthorizeProductLicense;
import com.vanysoft.business.system.licenseManager.service.LicenseManagerService;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名称：LicenseManagerAction
 * 类描述：License管理
 * 创建人：X
 * 创建时间：2017年9月17日下午11:37:24
 * 修改人：X
 * 修改时间：2017年9月17日
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/system/licenseManager")
public class LicenseManagerAction extends BaseController {

    @Autowired
    private LicenseManagerService licenseManagerService;

    @RequestMapping(value = "/queryProducts", method = RequestMethod.POST)
    public JSONResponse queryAuthorizeProductList(HttpServletRequest request)
            throws NumberFormatException, VanySoftBaseException {
        List<TAuthorizeProduct> list = licenseManagerService.queryAllAuthorizeProduct();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }

    @RequestMapping(value = "/queryLicense", method = RequestMethod.POST)
    public JSONResponse queryAuthorizeProductLicenseList(HttpServletRequest request, @SessionUser UserInfo userInfo, String scrollPageSize, String scrollPageIndex,
                                                         String searchValue, String productCode) throws NumberFormatException, VanySoftBaseException {
        Integer _pageIndex = StringUtil.isEmpty(scrollPageIndex) ? 1 : Integer.valueOf(scrollPageIndex);
        Integer _pageSize = StringUtil.isEmpty(scrollPageSize) ? 10 : Integer.valueOf(scrollPageSize);

        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            map = licenseManagerService.queryAuthorizeProductLicenses(_pageSize, _pageIndex, searchValue, productCode);
        }
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    @RequestMapping(value = "/createLicense", method = RequestMethod.POST)
    public JSONResponse createLicense(HttpServletRequest request, @Valid TAuthorizeProductLicense inputObj, BindingResult result) throws VanySoftBaseException {
        Map<String, Object> map = new HashMap<String, Object>();
        String license = licenseManagerService.createLicense(inputObj);
        map.put("license", license);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    @RequestMapping(value = "/checkLicense", method = RequestMethod.POST)
    public JSONResponse checkLicense(HttpServletRequest request, @Valid LicenseObj inputObj, BindingResult result) throws VanySoftBaseException {
        Map<String, Object> map = new HashMap<String, Object>();
        boolean b = licenseManagerService.checkLicense(inputObj);
        String isOk = "NO";
        if (b) isOk = "OK";
        map.put("isOk", isOk);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse addAuthorizeProductLicense(HttpServletRequest request, @Valid TAuthorizeProductLicense inputObj, BindingResult result) throws VanySoftBaseException {
        licenseManagerService.insertAuthorizeProductLicense(inputObj);
        return new JSONResponse();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse deleteAuthorizeProductLicense(HttpServletRequest request, String licenseId) throws VanySoftBaseException {
        licenseManagerService.deleteAuthorizeProductLicense(licenseId);
        return new JSONResponse();
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @VanySoftValidation
    public JSONResponse updateAuthorizeProductLicense(HttpServletRequest request, @Valid TAuthorizeProductLicense inputObj, BindingResult result) throws VanySoftBaseException {
        licenseManagerService.updateAuthorizeProductLicense(inputObj);
        return new JSONResponse();
    }

}
